import {
  Controller,
  Get,
  Post,
  Res,
  Body,
  Patch,
  Param,
  Delete,
  UseInterceptors,
  UploadedFile,
} from '@nestjs/common'
import { UploadService } from './upload.service'
import { CreateUploadDto } from './dto/create-upload.dto'
import { UpdateUploadDto } from './dto/update-upload.dto'
import {
  FileInterceptor,
  FilesInterceptor,
} from '@nestjs/platform-express/multer'
import { Response } from 'express' //必须从express导入Response
import { join } from 'path'
import { zip } from 'compressing'
@Controller('upload')
export class UploadController {
  constructor(private readonly uploadService: UploadService) {}

  @Post('album')
  @UseInterceptors(FileInterceptor('file')) //UseInterceptors这个装饰器用来处理文件 -- 传入参数FileInterceptor是读取字段名
  upload(@UploadedFile() file): string {
    //使用UploadedFile来接收file文件
    console.log(file, 'file')
    return '峰峰三十五岁憋不住了'
  }
  @Get('export')
  downLoad(@Res() res: Response): void {
    const url = join(__dirname, '../images/1667374567179.webp')
    res.download(url)
  }

  @Get('stream')
  async down(@Res() res: Response): Promise<void> {
    const url = join(__dirname, '../images/1667374567179.webp')
    const tarStream = new zip.Stream()
    await tarStream.addEntry(url)
    res.setHeader('content-type', 'application/octet-stream')
    res.setHeader('Content-Disposition', 'attachment; filename="Takagisam.zip"')

    tarStream.pipe(res)
  }

  // @Get()
  // findAll() {
  //   return this.uploadService.findAll();
  // }

  // @Get(':id')
  // findOne(@Param('id') id: string) {
  //   return this.uploadService.findOne(+id);
  // }

  // @Patch(':id')
  // update(@Param('id') id: string, @Body() updateUploadDto: UpdateUploadDto) {
  //   return this.uploadService.update(+id, updateUploadDto);
  // }

  // @Delete(':id')
  // remove(@Param('id') id: string) {
  //   return this.uploadService.remove(+id);
  // }
}
